#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
#import <LockInfo/Plugin.h>

#import "FWRoundedLabel.h"


@implementation FWRoundedLabel

@synthesize label, radius, indent, roundedColor;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
        self.clipsToBounds = NO;
        
        UIImage* glossImage = [UIImage imageWithContentsOfFile:
                               [[NSBundle bundleForClass:[self class]] pathForResource:@"gloss18" ofType:@"png"]];
        glossImage = [glossImage stretchableImageWithLeftCapWidth:glossImage.size.width / 2 - 1 topCapHeight:0];
        gloss = [[UIImageView alloc] initWithImage:glossImage];

        label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.opaque = YES;

        self.frame = frame;

        [self addSubview:label];
        [self addSubview:gloss];
        [self bringSubviewToFront:gloss];
    }
    
    return self;
}

- (void)dealloc
{
    [label release];
    [gloss release];
    [roundedColor release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Size management

- (CGFloat)indentForHeight:(CGFloat)height
{
    return height / 4;
}

- (CGFloat)radiusForHeight:(CGFloat)height
{
    return (int)(height / 2);
}

- (void)setFrame:(CGRect)newFrame
{
    CGRect oldFrame = self.frame;
    [super setFrame:newFrame];

    if ((newFrame.size.height != oldFrame.size.height) || (newFrame.size.width != oldFrame.size.width))
        [self layoutSubviews];
}

- (void)layoutSubviews
{
    CGRect rect = self.frame;
    
    radius = [self radiusForHeight:rect.size.height];
    indent = [self indentForHeight:rect.size.height];
    
    label.frame = CGRectMake(indent, 0, rect.size.width - 2 * indent, rect.size.height);
    gloss.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
}


// ******************************************************************************************
#pragma mark -
#pragma mark Properties access

- (void)setBackgroundColor:(UIColor *)newColor
{
    // Ignore - this prevents highlight of tableview changing the background color
}

- (CGSize)sizeForHeight:(CGFloat)height
{
    CGSize size = [label.text sizeWithFont:label.font];
    CGFloat newIndent = [self indentForHeight:height];
    
    return CGSizeMake(size.width + 2 * newIndent, height);
}


// ******************************************************************************************
#pragma mark -
#pragma mark Drawing

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    [roundedColor setStroke];
    [roundedColor setFill];

    rect = self.bounds;
    
    
    CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
    CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
    CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 4, M_PI / 2, 1);
    CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height);
    CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
    CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
    CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI / 2, 1);
    CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
    CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI / 2, M_PI, 1);
    
    CGContextFillPath(context);
}    

@end
